<!-- 多纵坐标图 -->
<template>
  <div id="muti-y-hetong" style="height: 100%; width: 100%"></div>
</template>

<script setup>
import * as echarts from 'echarts';
import { reactive, onMounted, watch } from 'vue';

const props = defineProps({ xAxis: Array, counts: Array, amounts: Array });

const colors = ['#5470C6', '#EE6666'];
const option = reactive({
  color: colors,
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross'
    }
  },
  grid: {
    left: '10%',
    top: '20%',
    bottom: '15%'
  },
  legend: {
    data: ['金额', '数量']
  },
  xAxis: [
    {
      type: 'category',
      axisTick: {
        alignWithLabel: true
      },
      axisLabel: {
        show: true,
        interval: 0,
        width: 50,
        overflow: "breakAll"
      },
      // prettier-ignore
      data: []
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: '金额（万元）',
      position: 'right',
      alignTicks: true,
      axisLine: {
        show: true,
        lineStyle: {
          color: colors[0]
        }
      },
      axisLabel: {
        formatter: '{value}'
      }
    },
    {
      type: 'value',
      name: '数量（个）',
      position: 'left',
      alignTicks: true,
      axisLine: {
        show: true,
        lineStyle: {
          color: colors[0]
        }
      },
      axisLabel: {
        formatter: '{value}'
      }
    }
  ],
  series: [
    {
      name: '金额',
      type: 'bar',
      barWidth: '40px',
      tooltip: {
        valueFormatter: function (value) {
          return value + ' 万元';
        }
      },
      data: [],
      label: {
        show: true,
        position: 'top',
        formatter: '{c}',
        z: 10 // 设置标注的层级
      },
    },
    {
      name: '数量',
      type: 'line',
      tooltip: {
        valueFormatter: function (value) {
          return value + ' 个';
        }
      },
      yAxisIndex: 1,
      data: []
    }
  ]
})
let mutiy = null;

watch(() => props, () => {
  option.xAxis[0].data = props.xAxis;
  option.series[0].data = props.amounts;
  option.series[1].data = props.counts;
  mutiy.setOption(option, true);
}, { deep: true })

onMounted(() => {
  mutiy = echarts.init(document.getElementById("muti-y-hetong"));

  option && mutiy.setOption(option);
});
</script>

<style scoped></style>
